<template>
  <panel-header :route="route"/>
  <el-table :data="accountData.list" style="width: 100%;margin-top: 10px">
    <el-table-column prop="id" label="id"/>
    <el-table-column prop="name" label="昵称"/>
    <el-table-column prop="permission_id" label="所属组别" >
      <template #default="scope">
        {{getPermissionName(scope.row.permissions_id)}}
      </template>
    </el-table-column>
    <el-table-column prop="mobile" label="手机号" />
    <el-table-column prop="active" label="状态" >
      <template #default="scope">
        <el-tag :type="scope.row.active?'success':'danger'">{{scope.row.active?'正常':'异常'}}</el-tag>

      </template>
    </el-table-column>
    <el-table-column  prop="create_time" label="创建时间">
      <template #default="scope">
        <div class="flex-box">
          <el-icon><Clock/></el-icon>
          <span style="margin-left: 10px">{{scope.row.create_time}}</span>
        </div>
      </template>
    </el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="primary" @click="open(scope.row)">编辑</el-button>
      </template>
    </el-table-column>
  </el-table>
  <div class="pagination-info">
    <el-pagination
        style="float:right"
        v-model:current-page="paginationData.pageNum"
        v-model:page-size="paginationData.pageSize"
        size="small"
        :background="false"
        layout="total,prev, pager, next"
        :total="accountData.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
    />
  </div>
  <el-dialog title="编辑"
             v-model="add"
             :before-close="beforeClose"
             width="500">
    <el-form ref="formRef"
             :model="form"
             :rules="rules"
             label-width="80px">
      <el-form-item label="电话" prop="mobile">
        <el-input v-model="form.mobile"  disabled/>
      </el-form-item>
      <el-form-item prop="name" label="名称">
        <el-input v-model="form.name" />
      </el-form-item>

      <el-form-item label="菜单权限" prop="permissions_id">
        <el-select
            v-model="form.permissions_id"
            placeholder="请选择菜单权限"
            style="width: 240px"
        >
          <el-option v-for="item in options"
                    :key="item.id"
                    :value="item.id"
                    :label="item.name">
          </el-option>
        </el-select>

      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="add = false;formRef=null">取消</el-button>
      <el-button type="primary" @click="confirm(formRef)">确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import {ref,reactive,onMounted} from "vue";
import {authAdmin,menuSelectList,updateUser} from "../../../api/index.js";
import dayjs from "dayjs";
import {Clock} from "@element-plus/icons-vue";
import PanelHeader from "../../../components/panelHeader.vue";
import {useRoute} from 'vue-router'

const route = useRoute()
//控制弹窗是否打开
const add=ref(false)
//表单数据
const form=reactive({
  name:'',
  mobile:'',
  permissions_id:[],
})

const formRef=ref(null)
const rules=reactive({
  name: [
    { required: true, message: '请输入昵称', trigger: 'blur' },
  ],
  permissions_id: [
    { required: true, message: '请选择权限', trigger: 'change' },
  ],
})
//表单提交
const confirm=async(formRef) => {
  if(!formRef) return
  await formRef.validate((valid, fields)=>{
    if(valid){
      const {name,permissions_id} = form;
      updateUser({name,permissions_id}).then(({data})=>{
        if(data.code ===10000){
          add.value=false;
          formRef.value=null;
          getAccountData(paginationData);

        }else{
          console.log(data.message)
        }
      })

    }else{
      console.log("error commit",fields)

    }
  })
}
//打开弹窗
const open=(row) => {
  add.value=true;
  Object.assign(form,{name:row.name,mobile:row.mobile,permissions_id:row.permissions_id})
}
//关闭弹窗
const beforeClose=() => {
  add.value=false;
}
//账号信息
const accountData=reactive({
  list:[],
  total:0
})

//分页数据
const paginationData=reactive({
  pageNum:1,
  pageSize:10
})
//改变分页
const handleSizeChange=(val) => {
  paginationData.pageSize=val
  getAccountData(paginationData)
}
const handleCurrentChange=(val) => {
  paginationData.pageNum=val
  getAccountData(paginationData)
}
//请求员工数据
const getAccountData=async (params) => {
  authAdmin(paginationData).then(({data})=>{

    accountData.list=data.data.list
    accountData.total=data.data.total
    accountData.list.forEach(item=>{item.create_time=dayjs(item.create_time).format('YYYY-MM-DD')})
  })
}

onMounted(() => {
  getAccountData(paginationData)
  //获取权限列表
  menuSelectList().then(({data})=>{
    options.value=data.data

  })
})

const options=ref([])
//根据权限id匹配权限名称
const getPermissionName=(id) => {
  const data =  options.value.find(item=>item.id===id)
  //新创建的用户默认为超级管理员
  return data?data.name:"超级管理员"
}

</script>
<style lang="less" scoped>
.flex-box{
  display: flex;
  align-items: center;
}
.pagination-info{
  height: 50px;
  line-height: 50px;
  padding-left:10px;
  background-color: white;
  text-align: right;
}
</style>
